package org.zerg.controller.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zerg.dto.FileDto;
import org.zerg.dto.Page;
import org.zerg.entity.Category;
import org.zerg.entity.Product;
import org.zerg.entity.ProductNote;
import org.zerg.service.CategoryService;
import org.zerg.service.ProductService;

@Controller
@RequestMapping("product")
public class ProductWebController {

	@Autowired
	private ProductService productService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private HttpSession session;
	
	@ResponseBody
	@RequestMapping("/bycate")
	public List<Product> findProductByCate(@RequestParam("id")int id) throws IOException{
		List<Product> list = productService.getProductByCa(id);
		return list;
	}
	
	@RequestMapping("/inser")
	public String inserProduct(Product product,FileDto dto){
		Product pr = productService.inserPoduct(product,dto);
		return "redirect:/category/detaile?id="+pr.getCategoryId();
	}
	
	@RequestMapping("/list")
	public String ProductByPage(@RequestParam("page")int page){
		List<Product> list = productService.getProductByPage(page);
		List<Category> list2 = categoryService.categoryAll();
		Page pa = productService.pageMessage(page);
		session.setAttribute("list", list);
		session.setAttribute("list2", list2);
		session.setAttribute("page", pa);
		return "/product";
	}
	
	@RequestMapping("/modify")
	public String deleteProduct(Product product,FileDto dto){
		if(dto==null||dto.getImgFile()==null||dto.getImgFile().getSize()<=0){
			productService.modifyProductNoFile(product);
		}else{
			productService.modifyProduct(product,dto);
		}
		double did = product.getId();
		double pag = did/15;
		int page = (int) Math.floor(pag)+1;
		List<Product> list = productService.getProductByPage(page);
		List<Category> list2 = categoryService.categoryAll();
		Page pa = productService.pageMessage(page);
		session.setAttribute("list", list);
		session.setAttribute("list2", list2);
		session.setAttribute("page", pa);
		return "/product";
	}
	
	@RequestMapping("/delete")
	public String deleteProduct(@RequestParam("id")int id){
		productService.deleteProduct(id);
		double did = id;
		double pag = did/15;
		int page = (int) Math.floor(pag)+1;
		List<Product> list = productService.getProductByPage(page);
		List<Category> list2 = categoryService.categoryAll();
		Page pa = productService.pageMessage(page);
		session.setAttribute("list", list);
		session.setAttribute("list2", list2);
		session.setAttribute("page", pa);
		return "/product";
	}
	
	@RequestMapping("/byimgs")
	public String productDeliteById(@RequestParam("id")int id){
		Product product = productService.getProductDelit(id);
		List<Category> list2 = categoryService.categoryAll();
		session.setAttribute("list2", list2);
		session.setAttribute("product", product);
		return "/product_image";
	}
	
	@RequestMapping("/inserimgs")
	public String inserProductImgs(FileDto dto,@RequestParam("id")int id,@RequestParam("order")int order){
		productService.inserImgs(dto,id,order);
		return "redirect:/product/byimgs?id="+id;
	}
	
	@RequestMapping("/modifyimgs")
	public String modifyProductImgs(FileDto dto,@RequestParam("id")int id,@RequestParam("order")int order,@RequestParam("classId")int classId){
		if(dto==null||dto.getImgFile()==null||dto.getImgFile().getSize()<=0){
			productService.modifyImgsNoFile(id,order,classId);
		}else{
			productService.modifyImgs(dto,id,order,classId);
		}
		return "redirect:/product/byimgs?id="+classId;
	}
	
	@RequestMapping("/deleteimgs")
	public String deleteProductImgs(@RequestParam("iid")int iid,@RequestParam("id")int id){
		productService.deleteImgs(iid);
		return "redirect:/product/byimgs?id="+id;
	}
	
	@RequestMapping("/byproper")
	public String productProperById(@RequestParam("id")int id){
		Product product = productService.getProductDelit(id);
		List<Category> list2 = categoryService.categoryAll();
		session.setAttribute("list2", list2);
		session.setAttribute("product", product);
		return "/product_property";
	}
	
	@RequestMapping("/inserproper")
	public String inserProductProper(@RequestParam("name")String name,@RequestParam("detail")String detail,@RequestParam("id")int id){
		productService.inserProper(name,detail,id);
		return "redirect:/product/byproper?id="+id;
	}
	
	@RequestMapping("/modifyproper")
	public String modifyProductProper(@RequestParam("id")int id,@RequestParam("name")String name,@RequestParam("detail")String detail,@RequestParam("classId")int classId){
		productService.modifyProper(id,name,detail,classId);
		return "redirect:/product/byproper?id="+classId;
	}
	
	@RequestMapping("/deleteproper")
	public String deleteProductProper(@RequestParam("iid")int iid,@RequestParam("id")int id){
		productService.deleteProper(iid);
		return "redirect:/product/byproper?id="+id;
	}
	
	@RequestMapping("/bynote")
	public String productNoteById(@RequestParam("id")int id){
		Product product = productService.getProductDelit(id);
		List<Category> list2 = categoryService.categoryAll();
		session.setAttribute("list2", list2);
		session.setAttribute("product", product);
		return "/product_note";
	}
	
	@RequestMapping("/insernote")
	public String inserProductNote(ProductNote note){
		productService.inserNote(note);
		return "redirect:/product/bynote?id="+note.getProductId();
	}
	
	@RequestMapping("/modifynote")
	public String modifyProductNote(ProductNote note){
		productService.modifyNote(note);
		return "redirect:/product/bynote?id="+note.getProductId();
	}
	
	@RequestMapping("deletenote")
	public String deleteProductNote(@RequestParam("id")int id,@RequestParam("productId")int productId){
		productService.deleteNote(id);
		return "redirect:/product/bynote?id="+productId;
	}
	
	@RequestMapping("isname")
	@ResponseBody
	public boolean isName(@RequestParam("name")String name){
		boolean result = productService.isName(name);
		return result;
	}
	
}
